package com.jhws.sys.appUpgrade.api;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.util.LogHelper;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.jhws.sys.appUpgrade.bo.TblSoftDimBo;
import com.jhws.core.db.BaseDao;
import com.jhws.common.base.bo.BaseJson;
import com.jhws.sys.appUpgrade.bo.TblSoftDimForm;

@Controller
@RequestMapping("/appupdate")
public class appUpdateApi {
    @Resource
    private OSSClientService ossClientService;
    private BaseDao dao = new BaseDao();

    @RequestMapping("outdoorUpdate.x")
    public void outdoorUpdate(HttpServletResponse response, String softLabel) {
        String equType = null;
        PrintWriter out = null;
        if (StringUtils.isEmpty(softLabel)) {
            softLabel = "000000";
        }
        try {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            // 这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            TblSoftDimForm TblSoftForm = new TblSoftDimForm();
            TblSoftDimBo tblSoftDimBo = dao.j().queryForObject(
                    "select * from tbl_soft_dim where code_vision =(select max(cast(code_vision as UNSIGNED INTEGER)) from tbl_soft_dim where equ_type= ? and soft_label = ? and (soft_state = '1' or soft_state is null)) and equ_type= ? and soft_label = ? and (soft_state = '1' or soft_state is null)",
                    new Object[]{"d", softLabel, "d", softLabel}, new PersonRowMapper());
            BeanUtils.copyProperties(TblSoftForm, tblSoftDimBo);

            TblSoftForm.setUrl(ossClientService.getUrl("appupdate/outdoor/" + softLabel + "/" + tblSoftDimBo.getSoftVision() + "/", true));
            equType = getEquType(TblSoftForm.getEquType());
            out.append(JSON.toJSONString(new BaseJson<>(TblSoftForm, equType, 1)));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            out.append(JSON.toJSONString(new BaseJson<>(e.toString(), equType, -1)));
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    @RequestMapping("mobileUpdate.x")
    public void mobileUpdate(HttpServletResponse response, String softLabel) {
        String equType = null;
        PrintWriter out = null;
        if (StringUtils.isEmpty(softLabel)) {
            softLabel = "000000";
        }
        try {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            // 这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            TblSoftDimForm TblSoftForm = new TblSoftDimForm();
            TblSoftDimBo tblSoftDimBo = dao.j().queryForObject(
                    "select * from tbl_soft_dim where code_vision =(select max(cast(code_vision as UNSIGNED INTEGER)) from tbl_soft_dim where equ_type= ? and soft_label = ? and (soft_state = '1' or soft_state is null)) and equ_type= ? and soft_label = ? and (soft_state = '1' or soft_state is null)",
                    new Object[]{"m", softLabel, "m", softLabel}, new PersonRowMapper());
            BeanUtils.copyProperties(TblSoftForm, tblSoftDimBo);
            TblSoftForm.setUrl(ossClientService.getUrl("appupdate/mobile/" + softLabel + "/" + tblSoftDimBo.getSoftVision() + "/", true));
            equType = getEquType(TblSoftForm.getEquType());
            out.append(JSON.toJSONString(new BaseJson<>(TblSoftForm, equType, 1)));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            out.append(JSON.toJSONString(new BaseJson<>(e.toString(), equType, -1)));
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    @RequestMapping("padUpdate.x")
    public void padUpdate(HttpServletResponse response, String softLabel) {
        String equType = null;
        PrintWriter out = null;
        if (StringUtils.isEmpty(softLabel)) {
            softLabel = "000000";
        }
        try {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            // 这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            TblSoftDimForm TblSoftForm = new TblSoftDimForm();
            TblSoftDimBo tblSoftDimBo = dao.j().queryForObject(
                    "select * from tbl_soft_dim where code_vision =(select max(cast(code_vision as UNSIGNED INTEGER)) from tbl_soft_dim where equ_type= ? and soft_label = ? and (soft_state = '1' or soft_state is null)) and equ_type= ? and soft_label = ? and (soft_state = '1' or soft_state is null)",
                    new Object[]{"p", softLabel, "p", softLabel}, new PersonRowMapper());
            BeanUtils.copyProperties(TblSoftForm, tblSoftDimBo);

            TblSoftForm.setUrl(ossClientService.getUrl("appupdate/pad/" + softLabel + "/" + tblSoftDimBo.getSoftVision() + "/", true));

            equType = getEquType(TblSoftForm.getEquType());
            out.append(JSON.toJSONString(new BaseJson<>(TblSoftForm, equType, 1)));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            out.append(JSON.toJSONString(new BaseJson<>(e.toString(), equType, -1)));
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    @SuppressWarnings("Duplicates")
    class PersonRowMapper implements RowMapper<TblSoftDimBo> {

        public TblSoftDimBo mapRow(ResultSet rs, int value) throws SQLException {
            TblSoftDimBo tblSoftDimBo = new TblSoftDimBo();
            tblSoftDimBo.setCodeVision(rs.getString("code_vision"));
            tblSoftDimBo.setEquType(rs.getString("equ_type"));
            tblSoftDimBo.setMandatoryUpgrade(rs.getString("mandatory_upgrade"));
            tblSoftDimBo.setSoftCode(rs.getString("soft_code"));
            tblSoftDimBo.setSoftFn(rs.getString("soft_file_name"));
            tblSoftDimBo.setSoftName(rs.getString("soft_name"));
            tblSoftDimBo.setSoftSize(rs.getString("soft_size"));
            tblSoftDimBo.setSoftState(rs.getString("soft_state"));
            tblSoftDimBo.setSoftVision(rs.getString("soft_vision"));
            tblSoftDimBo.setUpdExp(rs.getString("upd_exp"));
            return tblSoftDimBo;
        }
    }

    private String getEquType(String equType) {
        String retEquType = null;
        switch (equType) {
            case "p":
                retEquType = "pad";
                break;
            case "d":
                retEquType = "outdoor";
                break;
            case "m":
                retEquType = "mobile";
                break;
        }
        return retEquType;
    }
}
